﻿using System;
using System.Linq;

namespace MissingLinq.Linq2Management
{
	/// <summary>
	/// Linq2Management query extensions
	/// </summary>
	public static class QueryExtensions
	{
		/// <summary>
		/// Enables instance methods on objects returned from a Linq2Management query that affect an update  
		/// </summary>
		/// <typeparam name="T">The type of object being queried</typeparam>
		/// <param name="source">The query source</param>
		/// <returns>A collection whose objects are able to be updated</returns>
		public static IQueryable<T> ForUpdate<T>(this IQueryable<T> source) where T : class
		{
			var forUpdateMethod = source.GetType().GetMethod("ForUpdate", Type.EmptyTypes);
			if (forUpdateMethod != null)
				return (IQueryable<T>)forUpdateMethod.Invoke(source, null);

			return source;
		}
	}
}